package cn.hl.test;

import cn.hl.utils.ESClientUtil;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试Java操作ES：CRUD
 */
public class ES02 {
    
    @Test
    public void testAdd() throws Exception{
        // 1.获取连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备需要添加到ES的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",1);
        map.put("name","美杜莎");
        map.put("age",26);
        map.put("sex",0);
        // 3.链式语法添加
        client.prepareIndex("user", "employee", "1").setSource(map).get();
        // 4.关闭连接
        client.close();
    }

    @Test
    public void testGet() throws Exception{
        // 1.获取连接
        TransportClient client = ESClientUtil.getClient();
        // 2.链式语法查询
        Map<String, Object> source = client.prepareGet("user", "employee", "1").get().getSource();
        // 3.打印
        System.out.println(source);
        // 4.关闭连接
        client.close();
    }

    @Test
    public void testUpdate() throws Exception{
        // 1.获取连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备需要修改到ES的数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","云韵");
        // 3.链式语法修改
        client.prepareUpdate("user", "employee", "1").setDoc(map).get();
        // 4.关闭连接
        client.close();
    }

    @Test
    public void testDelete() throws Exception{
        // 1.获取连接
        TransportClient client = ESClientUtil.getClient();
        // 2.链式语法删除
        client.prepareDelete("user", "employee", "1").get();
        // 4.关闭连接
        client.close();
    }

    @Test
    public void testPatchAdd() throws Exception{
        // 1.获取连接
        TransportClient client = ESClientUtil.getClient();
        for (int i = 1; i < 40; i++) {
            // 2.准备需要添加到ES的数据
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", i);
            map.put("name", i % 2 == 0 ? "美杜莎" : "云韵");
            map.put("age", i);
            map.put("sex", i % 2);
            // 3.链式语法添加
            client.prepareIndex("user", "employee", "" + i).setSource(map).get();
        }
        // 4.关闭连接
        client.close();
    }

    /**
     * - 查询员工表
     * - name包含：我美杜莎
     * - age在10~30之间
     * - sex=1
     * - 每页大小10
     * - 从2页开始查
     * - 按照age倒序
     * @throws Exception
     */
    @Test
    public void testQuery() throws Exception{
        // 1.获取连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取查询对象
        SearchRequestBuilder builder = client.prepareSearch("user");
        // 3.指定要查询的类型
        builder.setTypes("employee");
        // 4.指定Query对象
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        // 4.1DSL查询
        boolQuery.must(QueryBuilders.matchQuery("name","美杜莎"));
        // 4.2DSL过滤
        boolQuery.filter(QueryBuilders.termQuery("sex",0));
        boolQuery.filter(QueryBuilders.rangeQuery("age").gte(10).lte(30));
        builder.setQuery(boolQuery);
        // 5.分页查询
        builder.setFrom(2);
        builder.setSize(10);
        // 6.排序
        builder.addSort("age", SortOrder.DESC);
        // 7.执行，得到结果集
        SearchHit[] hits = builder.get().getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }
        // 8.关闭连接
        client.close();
    }
}
